package cn.gzsxt.controller;

import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.annotation.SessionScope;

import cn.gzsxt.annotation.AdminSessionUpdate;
import cn.gzsxt.annotation.TokenForm;
import cn.gzsxt.service.ModularService;
import cn.gzsxt.utils.Global;
import cn.gzsxt.utils.Page;

@Controller
@SessionScope
@RequestMapping(value="/modular")
public class ModularController {
	
	private static final Logger LOGGER=LogManager.getLogger(ModularController.class);
	
	@Autowired
	private ModularService modularService;
	
	/**
	 * 实现模块增加
	 * @param entity
	 * @return
	 */
	@RequestMapping(value="/addModular")
	@TokenForm(remove=true)
	@AdminSessionUpdate
	public String  addModular(@RequestParam Map<String, Object> entity,HttpServletRequest request){
		LOGGER.debug("增加模块"+entity);
		Object sort = entity.get("MODULAR_SORT");
		//注意：空字符串无法转成一个整数类型
		if ("".equals(sort)) {
			entity.put("MODULAR_SORT", null);
		}
		//将模块的数据插入到数据库
		try {
			Map<String, Object> modular = modularService.addModular(entity);
			if (modular!=null) {
				request.setAttribute("modular_add_msg", "增加模块成功");
			}else{
				request.setAttribute("modular_add_msg", "增加模块失败");
			}
		} catch (Exception e) {
			request.setAttribute("modular_add_msg", "出现未知异常，请联系管理员");
			e.printStackTrace();
		}
		
		return "manager/modularAdd";
	}
	
	/**
	 * 删除模块记录
	 * @param modularId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/deleteModularById/{modularId}")
	@AdminSessionUpdate
	public String deleteModularById(@PathVariable Long modularId,HttpServletRequest request){
		
		//通过Id删除记录
		modularService.deleteModularByIds(modularId);
		
		//通过路径跳转回列表
		return "forward:/modular/toModularList/0";
	}
	
	/**
	 * 批量删除多条数据
	 * @return
	 */
	@RequestMapping(value="/deleteModularByIds")
	@AdminSessionUpdate
	public String deleteModularByIds(String[] modularId){
		LOGGER.debug("批量删除模块"+Arrays.toString(modularId));
		
		//通过模块编好数组删除记录
		modularService.deleteModularByIds((Object[])modularId);
		
		//通过路径跳转回列表
		return "forward:/modular/toModularList/0";
	}
	
	/**
	 * 更新模块记录
	 * @param entity
	 * @return
	 */
	@RequestMapping(value="/editModular")
	@AdminSessionUpdate
	public String editModular(@RequestParam Map<String, Object> entity,HttpServletRequest request){
		LOGGER.debug("更新模块："+entity);
		Object sort = entity.get("MODULAR_SORT");
		//注意：空字符串无法转成一个整数类型
		if ("".equals(sort)) {
			entity.put("MODULAR_SORT", null);
		}
		try {
			Map<String, Object> modular = modularService.editModular(entity);
			if (modular!=null) {
				request.setAttribute("modular_edit_msg", "更新模块成功");
				request.setAttribute("modular", modular);
			}else{
				request.setAttribute("modular_edit_msg", "更新模块失败");
			}
		} catch (Exception e) {
			request.setAttribute("modular_edit_msg", "出现未知异常，请联系管理员");
			e.printStackTrace();
		}
		return "manager/modularEdit";
	}
	

	
	/**
	 * 跳转到模块增加页面
	 * @return
	 */
	@RequestMapping(value="/toModularAdd")
	@TokenForm(create=true)
	public String toModularAdd(){
		LOGGER.debug("跳转到模块增加页面");
		return "manager/modularAdd";
	}
	
	/**
	 * 跳转到模块编辑页面，编辑数据必须要标识唯一的记录
	 * 所以必须要传递一个模块编号过来
	 * @return
	 */
	@RequestMapping(value="/toModularEdit/{modularId}")
	public String toModularEdit(@PathVariable Long modularId,HttpServletRequest request){
		LOGGER.debug("跳转到编辑模块页面"+modularId);
		//跳转到编辑页面之前，需要将要编辑的模块数据查询出来
		Map<String, Object> modular = modularService.findModularById(modularId);
		request.setAttribute("modular", modular);
		
		return "manager/modularEdit";
	}
	
	/**
	 * 跳转到模块列表页面
	 * @param index 数据库索引=页面索引-1。
	 * @param condition 查询的条件
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/toModularList/{index}")
	public String toModularList(@PathVariable Integer index,@RequestParam Map<String, Object> condition, HttpServletRequest request) {
		LOGGER.debug("跳转到模块列表");
		if (index==null) {
			index=0;
		}
		//第一步：实现全部模块数据查询
		Page page = modularService.findModularByConditionToPage(condition, index, Global.RECORD_SIZE);
		//第二步：将数据通过作用域返回到页面
		request.setAttribute("modularPage", page);
		
		return "manager/modularList";
	}

}
